package cn.cheneyjoo.controller;

import cn.cheneyjoo.DTO.ResponseDTO;
import cn.cheneyjoo.Model.Product;
import cn.cheneyjoo.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 获取一个商品信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product getOneProduct(@PathVariable Integer id) throws InterruptedException {
        System.out.println(id);
        Thread.sleep(3000);
        Product product = goodsService.getOneProduct(id);
        return product;
    }

    /**
     * 删除商品信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseDTO deleteProduct(@PathVariable Integer id) {
        boolean b = goodsService.deleteProduct(id);
        ResponseDTO responseDTO = new ResponseDTO(b ? 200 : 203, b ? "成功" : "失败", null);
        return responseDTO;
    }

    /**
     * 保存商品信息
     *
     * @param product
     * @return
     */
    @PostMapping()
    public ResponseDTO saveProduct(Product product) {
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        boolean b = goodsService.saveProduct(product);
        ResponseDTO responseDTO = new ResponseDTO(b ? 200 : 203, b ? "成功" : "失败", null);
        return responseDTO;
    }

    /**
     * 分页查询
     *
     * @param pageIndex 页码
     * @param pageSize  每页条数
     * @return
     */
    @GetMapping("/page/{pageIndex}")
    public ResponseDTO getProductsByPage(@PathVariable int pageIndex, int pageSize, String ProductName,
                                         String ProductType,String ProductMinPrice,String ProductMaxPrice,
                                         String ProductMinStock,String ProductMaxStock,String ProductStatus) {
        //拼接查询参数
        Map<String, String> where=new HashMap<>();
        if (Optional.ofNullable(ProductName).isPresent()) {
            where.put("ProductName",ProductName);
        }
        if (Optional.ofNullable(ProductType).isPresent()) {
            where.put("ProductType",ProductType);
        }
        if (Optional.ofNullable(ProductMinPrice).isPresent()) {
            where.put("ProductMinPrice",ProductMinPrice);
        }
        if (Optional.ofNullable(ProductMaxPrice).isPresent()) {
            where.put("ProductMaxPrice",ProductMaxPrice);
        }
        if (Optional.ofNullable(ProductMinStock).isPresent()) {
            where.put("ProductMinStock",ProductMinStock);
        }
        if ( Optional.ofNullable(ProductMaxStock).isPresent()) {
            where.put("ProductMaxStock",ProductMaxStock);
        }
        if (Optional.ofNullable(ProductStatus).isPresent()) {
            where.put("ProductStatus",ProductStatus);
        }
        Page products = goodsService.getProductsByPage(pageIndex, pageSize, where);
        ResponseDTO responseDTO = new ResponseDTO<Page>(200, "成功", products);
        return responseDTO;
    }
}
